//
//  RecommendViewModel.swift
//  CWLiving
//
//  Created by 方正刚 on 2017/7/6.
//  Copyright © 2017年 方正刚. All rights reserved.
//

import UIKit

private let HOT_URL = "https://capi.douyucdn.cn/api/v1/getBigDataRoom"

/// 后面分组的URL
private let GROUP_URL = "https://capi.douyucdn.cn/api/v1/getHotCate"

/// 颜值数据URL
private let PRETTY_URL = "https://capi.douyucdn.cn/api/v1/getVerticalRoom"

/// 轮播数据URL
private let CYCLE_URL = "https://capi.douyucdn.cn/api/v1/slide/6"

private let CYCLE_PARAMTERS = ["version" : "2.520"]

/// 请求颜值数据 和 后面几组数据的参数
private let PARAMETERS = ["limit" : "4", "offset" : "0", "time" : NSDate.getCurrentTime()]

class RecommendViewModel: BaseViewModel {
    
    
    /// 颜值
    lazy var prettys: [AnchorModel] = [AnchorModel]()
    
    /// 最热
    lazy var hots: [AnchorModel] = [AnchorModel]()
    
    /// 轮播数据
    lazy var Cycles: [CycleModel] = [CycleModel]()
}


extension RecommendViewModel {
    
    /// 请求列表展示数据
    ///
    /// - Parameter finishedCallback: 请求完成的回调
    func requestData(finishedCallback : @escaping () -> ()) {
    
        
    /// 创建线程组
    let dDroup = DispatchGroup()
     
    // 开始请求第一部分最热的数据
    dDroup.enter()
    // 最热的URL
        NetworkTools.requestData(type: .GET, URLSrting: HOT_URL, parameters: ["time" : NSDate.getCurrentTime()]) { (result) in
            
            guard let resultDic = result as? [String : NSObject] else {return}
            
            guard let dataArr = resultDic["data"] as? [[String : NSObject]] else {return}
            
            for dic in dataArr {
                
                let anchorModel = AnchorModel(dict: dic)
                
                self.hots.append(anchorModel)
                
            }
            

            // 离开组
            dDroup.leave()
        }
     
      dDroup.enter()
     // 请求颜值数据
        NetworkTools.requestData(type: .GET, URLSrting: PRETTY_URL, parameters: PARAMETERS) { (result) in
            guard let resultDic = result as? [String : NSObject] else {return}
            
            guard let dataArr = resultDic["data"] as? [[String : NSObject]] else {return}
            
            for dic in dataArr {
                
                let anchorModel = AnchorModel(dict: dic)
                
                self.prettys.append(anchorModel)
                
            }
            
            // 离开组
            dDroup.leave()

        }
        
        // 请求后面几组的数据
        dDroup.enter()
        loadAnchorData(URLSrting: GROUP_URL, paramters: PARAMETERS) { 
            // 离开组
            dDroup.leave()

        }
        
    dDroup.notify(queue: DispatchQueue.main) {
        
        let hotGropModel = AnchorGroupModel()
        hotGropModel.room_listModels = self.hots
        hotGropModel.tag_name = "最热"
        hotGropModel.icon_name = "home_header_hot"
        
        let prettyModel = AnchorGroupModel()
        prettyModel.room_listModels = self.prettys
        prettyModel.tag_name = "颜值"
        prettyModel.icon_name = "home_header_phone"
        
        
        self.anchorGroups.insert(prettyModel, at: 0)
        
        self.anchorGroups.insert(hotGropModel, at: 0)
        
        finishedCallback()
    }

   }
    
    /// 请求轮播数据
    ///
    /// - Parameter finishedCallback: 请求完成的回调
    func requestCycleData(finishedCallback: @escaping () ->()){
        
        NetworkTools.requestData(type: .GET, URLSrting: CYCLE_URL, parameters: CYCLE_PARAMTERS) { (result) in
            
            /// 获取字典数据
            guard let resultDict = result as? [String : NSObject] else {return}
            
            
            /// 获取data对应的数据
            guard let dataArr = resultDict["data"] as? [[String : NSObject]] else {return}
            
            for dict in dataArr {
                
               self.Cycles.append(CycleModel(dict: dict))
            }
            
            finishedCallback()
        }
        
    }
    
}
